/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package digitaldreamers.libraries.games.servers.networkmanagement.abstracts;

import digitaldreamers.libraries.games.servers.networkmanagement.packets.sending.AbSendablePacket;

/**
 * An interface that presents the connection handler.
 *
 * @author DaoLe
 */
public interface IConnectionHandler {
    
    public static enum ConnectionStates {
         INITIAL
        ,STARTED
        ,RUNNING
        ,STOPPED
    }
    
    /**
     * An interface that presents connection error event.
     */
    public static interface IOnConnectionErrorListener {

        /**
         * The callback function. It is called when connection error occurred.
         *
         * @param pConnectionHandler The ConnectionHandler object that raised the event.
         * @param pState The connection state.
         */
        void onConnectionError(IConnectionHandler pConnectionHandler, ConnectionStates pState);
    }

    /**
     * Register a new listener for the connection error event.
     *
     * @param pListener The new listener of the event.
     * @return The result whether or not the listener is registered.
     */
    boolean addConnectionErrorListener(IOnConnectionErrorListener pListener);

    /**
     * Unregister a listener for the connection error event.
     *
     * @param pListener The registered listener of the event.
     * @return The result whether or not the listener is unregistered.
     */
    boolean removeConnectionErrorListener(IOnConnectionErrorListener pListener);
    
    /**
     * Fire the connection error event.
     *
     */
    void fireConnectionErrorEvent();

    /**
     * Get the game server.
     * 
     * @return The game server.
     */
    IGameServer getGameServer();
    
    /**
     * Get the player of this connection.
     *
     * @return The player of this connection.
     */
    IPlayerComponent getPlayer();

    /**
     * Set the player of this connection.
     *
     * @param pPlayer The player of this connection.
     */
    void setPlayer(IPlayerComponent pPlayer);

    /**
     * Send data to client.
     *
     * @param pSendablePacket The data to send.
     */
    void send(AbSendablePacket pSendablePacket);
    
    /**
     * Start this connection thread.
     */
    void start();
    
    /**
     * Stop this connection thread.
     */
    void stop();
    
    /**
     * Show message.
     * @param pMessage The message to show.
     */
    void showMessage(String pMessage);
}
